package com.java46.struts2.FoodType.dao;

import com.java46.struts2.base.entity.TDishestypeEntity;
import com.java46.struts2.base.entity.vo.TypeFoods;
import com.java46.struts2.base.exception.BusinessException;
import com.java46.struts2.base.util.HibernateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;
/**
 * @ClassName FoodTypeDao
 * @Description 菜品类型的CRUD
 * @Author Tigger Wu
 * @Date 2018/7/16 19:33
 * @Version 1.0
 **/
public class FoodTypeDao {
    /*
     *  添加菜品类型
     *
     * @author Tigger Wu
     * @date 2018/7/17 9:43
     * @param
     * @return
     */
    public void addFoodType(String name){
        try {
            Session session = HibernateUtils.getSession();
            Transaction transaction = session.beginTransaction();
            TDishestypeEntity type = new TDishestypeEntity();
            type.setdTypeId(0);
            type.setdTypeName(name);
            session.save(type);
            transaction.commit();
            session.close();
        }catch (Exception e){
            throw new BusinessException("xx");
        }

    }
    /*
     *  查询全部菜品类型
     *
     * @author Tigger Wu
     * @date 2018/7/17 9:43
     * @param
     * @return
     */

    Logger loger = LogManager.getLogger(FoodTypeDao.class);
    public List<TypeFoods> queryAll(){
        loger.error("第二处开始");
        List<TypeFoods>dlist = null;
        try {
            Query query = HibernateUtils.getQuery("from TypeFoods");
            dlist = query.list();
        }catch (Exception e){
            throw  new BusinessException("查询失败！", e);
        }finally {
        }
        System.out.println(dlist.size());
        loger.error("第二处结束");
        return dlist;
    }
    /*
     *  删除菜品类型
     *
     * @author Tigger Wu
     * @date 2018/7/17 9:46
     * @param id
     * @return
     */

    public void removeType(int id){
        try {
            Session session = HibernateUtils.getSession();
            Transaction tran = session.beginTransaction();
            TDishestypeEntity type = session.get(TDishestypeEntity.class,id);
            session.delete(type);
            tran.commit();
            session.close();
            }catch (Exception e){
            throw new BusinessException("cc");
        }
  }
    /*
     *  修改菜品类型
     *
     * @author Tigger Wu
     * @date 2018/7/17 10:11
     * @param
     * @return
     */
    public void updateType(TDishestypeEntity type){
        try {
            Session session = HibernateUtils.getSession();
            Transaction tran = session.beginTransaction();
            TDishestypeEntity types = session.get(TDishestypeEntity.class,type.getdTypeId());
            types.setdTypeId(type.getdTypeId());
            types.setdTypeName(type.getdTypeName());
            session.update(types);
            tran.commit();
            session.close();
        }catch (Exception e){
            throw new BusinessException("aa");
        }
    }

    /**
     *@Author xiao
     *@Description //分页查询菜品类型可添加关键字
     *@Date 10:01 2018/7/18
     *@Param []
     *@return List
     */
    public static List queryFoodType(String Keyword,int Pageno,int Pagesize){
        String hql="";
        Query query=null;
        if(Keyword==null||Keyword.trim().equals("")){
            hql="from TDishestypeEntity";
        }else {
            hql="from TDishestypeEntity where dTypeName like '%"+Keyword+"%'";
        }
        query=HibernateUtils.getQuery(hql).setFirstResult((Pageno-1)*Pagesize).setMaxResults(Pagesize);
        List list = query.list();
        return list;
    }

    /**
     *@Author xiao
     *@Description //查询数据总条数
     *@Date 19:27 2018/7/18
     *@Param [Keyword]
     *@return int
     */
    public static int queryTypesCount(String Keyword){
        String hql="";
        Query query=null;
        if(Keyword==null||Keyword.trim().equals("")){
            hql="select count(*) from TDishestypeEntity";
        }else {
            hql="select count(*) from TDishestypeEntity where dTypeName like '%"+Keyword+"%'";
        }
        query=HibernateUtils.getQuery(hql);
        Object o = query.uniqueResult();
        return Integer.parseInt(o.toString());
    }

    /**
     *@Author xiao
     *@Description //根据菜品类型编号删除菜品类型
     *@Date 15:04 2018/7/18
     *@Param [orderid]
     *@return void
     */
    public static void deleteType(int Typeid){
        Session session=HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        String hql="delete from TDishesEntity where dTypeId=?1";
        Query query=session.createQuery(hql);
        query.setParameter(1,Typeid);
        query.executeUpdate();
        String hql1="delete from TDishestypeEntity where dTypeId=?1";
        query=session.createQuery(hql1);
        query.setParameter(1,Typeid);
        query.executeUpdate();
        transaction.commit();
        session.close();
    }


    /**
     * @Author xiaobo
     * @Description //添加菜品类型
     * @Date 17:01 2018/7/22 0022
     * @Param [Typename]
     * @return void
     */
    public static void addType(String Typename){
        Session session=HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        TDishestypeEntity tDishestypeEntity = new TDishestypeEntity();
        tDishestypeEntity.setdTypeName(Typename);
        tDishestypeEntity.setdTypeId(0);
        session.save(tDishestypeEntity);
        transaction.commit();
        session.close();
    }

}
